package com.cqcetbbs.smartlab.cqcetbbs.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.cqcetbbs.smartlab.cqcetbbs.R;
import com.cqcetbbs.smartlab.cqcetbbs.adapter.NewlistAdapter;
import com.cqcetbbs.smartlab.cqcetbbs.javabean.HotArt;
import com.cqcetbbs.smartlab.cqcetbbs.view.MytitleView;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by user on 2017/10/31.
 */
public class NewsListActivity extends AppCompatActivity {

    OkHttpClient client;
    private static String HOT_NEWS_PATH = "http://api.cqcetlab.com/Home/article/hotart?num=";

    public static final int HOT_NEWS_SUCCESS = 1;
    public static final int HOT_NEWS_FAIL = 0;
    ArrayList<HotArt.DataEntity> hotArtArrayList;
    ArrayList<HotArt.DataEntity> DataArrayList;
    HotArt hotArt;
    //    ArrayList<HotArt> noticeArrayList;
//    ArrayList<HotArt> clubArrayList;
//    ArrayList<HotArt> tradeArrayList;
//    ArrayList<HotArt> jobArrayList;
    private ListView lv_newlist;
    String type;
    public Handler mhandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case HOT_NEWS_SUCCESS:
                    Gson gson = new Gson();
                    hotArt = new HotArt();
                    hotArt = gson.fromJson((String) msg.obj, HotArt.class);

                    //得到传入类型的数据
                    ArrayList<HotArt.DataEntity> data = getData(type);
                    initData(data);
                    break;
                case HOT_NEWS_FAIL:
                    Toast.makeText(NewsListActivity.this, "网络异常！", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_newlist);
        Intent intent = getIntent();
        type = intent.getStringExtra("type");
        initView();
        requestData("20");


    }

    private void initData(ArrayList<HotArt.DataEntity> listInfo) {
        NewlistAdapter NewlistAdapter = new NewlistAdapter(listInfo, this);
        lv_newlist.setAdapter(NewlistAdapter);
        lv_newlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Bundle bundle = new Bundle();
                bundle.putString("id", DataArrayList.get(i).getId());
                Intent intent = new Intent(NewsListActivity.this, NewsDetailActivity.class);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }

    private void initView() {
        MytitleView mytitle = (MytitleView) findViewById(R.id.mv_title);
        mytitle.setTitle(type);
        DataArrayList = new ArrayList<>();
        lv_newlist = (ListView) findViewById(R.id.lv_newlist);

        mytitle.setonBackListener(new MytitleView.onBackListener() {
            @Override
            public void onBack() {
                NewsListActivity.this.finish();
            }
        });

    }

    private void requestData(final String type) {
        client = new OkHttpClient();
        final String state = type;
        new Thread(new Runnable() {
            @Override
            public void run() {
                Request request = new Request.Builder()
                        .get()
                        .url(HOT_NEWS_PATH + state)
                        .build();
                Response response = null;
                try {
                    //回调
                    response = client.newCall(request).execute();
                    if (response.isSuccessful()) {
                        //将服务器响应的参数response.body().string())发送到hanlder中，并更新ui
                        String result = response.body().string();
                        //将数据保存
                        //SpUtils.setStringValues(activity, SCHOOLNEWS, result);
                        Message msg = mhandler.obtainMessage(HOT_NEWS_SUCCESS, result);
                        mhandler.sendMessage(msg);
                    }
                } catch (IOException e) {
                    Message msg = mhandler.obtainMessage(HOT_NEWS_FAIL);
                    mhandler.sendMessage(msg);
                    e.printStackTrace();
                }
            }
        }).start();
    }

    public ArrayList<HotArt.DataEntity> getData(String type) {

        if (type.equals("life")) {
            loopResult("life");
        }
        if (type.equals("notice")) {
            loopResult("notice");
        }
        if (type.equals("club")) {
            loopResult("club");
        }
        if (type.equals("trade")) {
            loopResult("trade");
        }
        if (type.equals("job")) {
            loopResult("job");
        }
        if (type.equals("study")) {
            loopResult("study");
        }
        return DataArrayList;
    }


    public ArrayList<HotArt.DataEntity> loopResult(String type) {
        DataArrayList.clear();
        ArrayList<HotArt.DataEntity> data = hotArt.getData();
        for (HotArt.DataEntity hotArt : data) {
            if (hotArt.getType().equals(type)) {
                DataArrayList.add(hotArt);
            }
        }
        return DataArrayList;
    }
}
